import VueRouter from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('./views/IndexView.vue'),
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('./views/LoginView.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  routes,
})

router.beforeEach((to, from, next) => {
  if (localStorage.getItem('token')) {
    // 已登录
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    //未登录
    if (to.path === '/login') {
      // 不需要认证
      next()
    } else {
      next({ path: '/login' })
    }
  }
})

export default router
